<?php

class AliyunOSSHelper{
	
	private $config = null;
	function __construct($cfg){
		$this->config = $cfg;
	}
	
	//上传本地图片(本地绝对路径，远程保存路径[无需考虑文件夹递归]，上传成功后是否删除本地备份)
	public function uploadLocal($localPath,$remotePath,$deletelocal=false){
		
		if($localPath == ''){
			return ['flag'=>false,'info'=>'本地路径不能为空.'];
		}
		
		//echo mb_detect_encoding($localPath,['ASCII','UTF-8','GBK','GB2312']);

		$t_localPath = iconv('gbk','utf-8',$localPath);

		if($remotePath == ''){
			return ['flag'=>false,'info'=>'远程路径不能为空'];
		}
		
		$url = '';
		try{
			Vendor('aliyunoss.Image');
			\Image::Init($this->config);
			$url = \Image::UploadFile($t_localPath,$remotePath);
		}catch(Exception $e){
			__log($e->getMessage(),'aliyunoss');
		}
		if($url == ''){
			$result = ['flag'=>false,'info'=>'上传失败.'];
		}else{
			if($deletelocal){
				@unlink($localPath);
			}
			$result = ['flag'=>true,'info'=>'success','data'=>$url];
		}
		return $result;
	}
	
	
	//删除图片
	public function DeleteImage($remotepath){
		
		$data = null;
		try{
			Vendor('aliyunoss.Image');
			\Image::Init($this->config);
			$data = \Image::DeleteFile($remotePath);
		}catch(\Exception $e){
			__log($e->getMessage(),'aliyunoss');
		}
		return $data;
	}
	
	
}